package com.htandroid.htandroid.excel;

import android.content.Context;

import com.htandroid.htandroid.entity.UffjqgdList;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;

import java.io.File;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelPurchase_bmqgsp extends BaseExcel {
    private static WritableWorkbook wwb;
    private static String excelPath;
    private static String listtype;
    private static File excelFile;
    private static List<UffjqgdList> list;

    public ExcelPurchase_bmqgsp(Context context, String excelPath, List<UffjqgdList> list, String listtype) {
        this.excelPath = excelPath;
        this.list = list;
        this.listtype = listtype;
        excelFile = new File(excelPath);
        myApplication = (MyApplication) context.getApplicationContext();
        createSmtrautExcel(myApplication, excelFile, list, listtype);
    }

    public static void createSmtrautExcel(MyApplication myApplication, File file, List<UffjqgdList> list, String listtype) {
        format();
        WritableSheet ws = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            wwb = Workbook.createWorkbook(file);
            ws = wwb.createSheet("0".equals(listtype) ? "待审核" : "已审核", 0);
            String[] topic = {"单据号", "部门", "金额", "请购流程", "请购日期", "制单人",
                    "定价人", "已审步骤", excelPath.contains("待") ? "待审角色" : "已审角色"};

            Label label = new Label(0, 0, myApplication.getGs_ztname(), arial14format);
            ws.addCell(label);

            for (int i = 0; i < topic.length; i++) {
                Label labelC = new Label(i, 1, topic[i], arial12format);
                try {
                    ws.addCell(labelC);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Label labelCid = new Label(0, i + 2, list.get(i).getCvouchid());
                Label labelCdeptName = new Label(1, i + 2, list.get(i).getCdepname());
                Label labelCIamount = new Label(2, i + 2, Basenvo.customFormat("######.00##", list.get(i).getIamount()));
                Label labelLinename = new Label(3, i + 2, list.get(i).getLinename());
                Label labelCdate = new Label(4, i + 2, list.get(i).getDdate());
                Label labelCmaker = new Label(5, i + 2, list.get(i).getCmaker());
                Label labelCprechecker = new Label(6, i + 2, list.get(i).getCprechecker());
                Label labelCdefine4 = new Label(7, i + 2, list.get(i).getCdefine4());
                Label labelCchar = new Label(8, i + 2, list.get(i).getCchar());
                try {
                    ws.addCell(labelCid);
                    ws.addCell(labelCdeptName);
                    ws.addCell(labelCIamount);
                    ws.addCell(labelLinename);
                    ws.addCell(labelCdate);
                    ws.addCell(labelCmaker);
                    ws.addCell(labelCprechecker);
                    ws.addCell(labelCdefine4);
                    ws.addCell(labelCchar);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            ws.mergeCells(0, 0, topic.length - 1, 0);
            ws.setColumnView(0, 15);
            ws.setColumnView(1, 15);
            ws.setColumnView(3, 25);
            ws.setColumnView(4, 15);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wwb != null) {
                try {
                    wwb.write();
                    wwb.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
